using System;
using SharePoint.Security.ConfigAnalyzer.Engine.Base;
using SharePoint.Security.ConfigAnalyzer.Engine.Common;
using Microsoft.SharePoint.Administration;

namespace SharePoint.Security.ConfigAnalyzer.Engine.Rules.Others
{
    public class FarmServices : BaseRule
    {
        public FarmServices()
        {
            RuleInfo = new RuleInfo
                           {
                               Title = "Farm Services",
                               Description = "Services running on different farm nodes",
                               ExpectedValue = "Should have limited number of services running on farm"
                           };
        }

        #region Overrides of BaseRule

        public override void Validate()
        {
            SPFarm farm = ObjectHelper.Farm;
            
            foreach (SPServer server in farm.Servers)
            {
                CurrentValue = CurrentValue + Environment.NewLine + string.Format("Services on {0}:", server.Name);

                foreach (SPServiceInstance srv in server.ServiceInstances)
                {
                    if (srv.Status == SPObjectStatus.Disabled || srv.Status == SPObjectStatus.Offline)
                    {
                        continue;
                    }

                    CurrentValue = CurrentValue + Environment.NewLine + string.Format("{0}-[Status:{1}]", srv.TypeName, srv.Status);
                }
            }

            Status  = Status.InConclusive;
        }

        #endregion
    }
}